package com.logistic;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.Entity.Product.Product;
import com.Entity.Product.ProductImplement;
import com.Entity.Visitor.Visitor;
import com.Tool.RandomStringCreater;

public class FileManager extends Basic implements ServletResponseAware{
	private static final long serialVersionUID = 1L;
	/**
	 * @Title FileManager.java
	 * @Package com.logistic
	 * @author ufo
	 */

	private File upload;
	private String uploadContentType;
	private String uploadFileName;
	private String savePath;
	private String baseUrl;
	private HttpServletResponse response;
	private InputStream inputStream;
	private String[] productId;

	public String upload(){
		try {
			String name;
			addSessionToVisitor();
			name=this.visitor.getVisitorId();

			FileOutputStream fos=null;
			boolean flag=true;
			do{
				name+=RandomStringCreater.createWithLettersAndNumbers(10);
				if(!(new File(getSavePath()+name).exists())){
					fos=new FileOutputStream(getSavePath()+name);
					flag=false;
				}
			}while(flag);
			FileInputStream fis=new FileInputStream(getUpload());

			byte[] buffer=new byte[1024];
			int len=0;
			while((len=fis.read(buffer))>0)
				fos.write(buffer,0,len);

				response.setContentType("text/html; charset=UTF-8");  
				response.setHeader("Cache-Control", "no-cache");  
				PrintWriter out = response.getWriter();
				String callback =ServletActionContext.getRequest().getParameter("CKEditorFuncNum");   
			    out.println("<script type=\"text/javascript\">");  
			    out.println("window.parent.CKEDITOR.tools.callFunction("+ callback + ",'" +getBaseUrl()+"?image="+name+ "','')");   
			    out.println("</script>");   
				out.flush();
				out.close();
				return null;
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public String addOneProductImage(){
		try{
			String name;
			addSessionToVisitor();
			name=this.visitor.getVisitorId();

			FileOutputStream fos=null;
			boolean flag=true;
			do{
				name+=RandomStringCreater.createWithLettersAndNumbers(10);
				if(!(new File(getSavePath()+name).exists())){
					fos=new FileOutputStream(getSavePath()+name);
					flag=false;
				}
			}while(flag);
			FileInputStream fis=new FileInputStream(getUpload());

			byte[] buffer=new byte[1024];
			int len=0;
			while((len=fis.read(buffer))>0)
				fos.write(buffer,0,len);

			ProductImplement product=new Product(getProductId()[0]);
			int i=1;

			for(;i<6&&product.getValue("image"+i)!=null;i++)
				System.out.println(i);
			if(i<6){

				product.addKey("image"+i, getBaseUrl()+"?image="+name);
				product.save();

				response.setContentType("text/html; charset=UTF-8");  
				response.setHeader("Cache-Control", "no-cache"); 
				System.out.println(123456);
				PrintWriter out = response.getWriter();
			    out.println("<html><body><script type=\"text/javascript\">");  
			    out.println("window.location.href='productOwner.html'");   
			    out.println("</script></body></html>");   
				out.flush();
				out.close();
				return null;
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return SUCCESS;
	}
	public String download(){
		try {
			String name=request.getParameterValues("image")[0];
			if(!name.isEmpty()){
				FileInputStream fis=new FileInputStream(getSavePath()+name);
				setInputStream(fis);
			}
			return SUCCESS;
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			return ERROR;
		}
	}

	public String getSavePath() {
		return savePath;
	}

	@Override
	public void addSessionToVisitor() {
		// TODO 自动生成的方法存根
		visitor=new Visitor((String)request.getSession().getAttribute("visitorName")
				,(String)request.getSession().getAttribute("password"));
	}

	@Override
	public Map<String, String> getResult() {
		// TODO 自动生成的方法存根
		return this.result;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		// TODO 自动生成的方法存根
		this.response=response;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public File getUpload() {  
		return upload;  
    }  

	public void setUpload(File upload) {  
    	this.upload = upload;  
    }

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getBaseUrl() {
		return baseUrl;
	}

	public void setBaseUrl(String baseUrl) {
		this.baseUrl = baseUrl;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public InputStream getImage() {
		return getInputStream();
	}

	public String[] getProductId() {
		return productId;
	}

	public void setProductId(String[] productId) {
		this.productId = productId;
	}
}
